<template>
  <div class="userSelect">
    <div class="select" @click="onSelect()" v-show="!showRadio">
      <span v-for="(item, index) in selectList" :key="index">{{item}} <i @click.stop="onDelete(index)">x</i></span>
      <img src="../../assets/img/login/icon_right.png" alt="">
    </div>
    <label for="loginSeclet" @click="onShowSeclet()" v-show="showRadio">
      <input id="loginSeclet" type="text" disabled v-model="selectText">
      <img src="../../assets/img/login/icon_right.png" alt="">
    </label>
    <div class="option" v-show="showOptionFlag">
      <div class="optionItem" v-for="(item, index) in option" :key="index" @click="onOption(item.label, item.value)">{{item.label}}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'userSelect',
  props: {
    option: {
      type: Array,
      default () {
        return [{
          label: 'logo设计',
          value: 1
        }]
      }
    },
    showRadio: {
      type: Boolean,
      default () {
        return false
      }
    }
  },
  data () {
    return {
      showOptionFlag: false,
      selectList: [],
      selectText: '',
      selectListIndex: [],
      optionList: [],
      bubblingFlag: false // 阻止冒泡
    }
  },
  created () {
    if (typeof this.option[0] === 'string') {
      this.optionList = []
      for (let i = 0; i < this.option.length; i++) {
        this.optionList.push({
          label: this.option[i],
          value: i
        })
      }
    } else {
      this.optionList = this.option
    }
  },
  methods: {
    // 显示下拉框
    onShowSeclet () {
      this.bubblingFlag = true
      this.globalClick(() => {
        if (this.bubblingFlag === true) {
          this.bubblingFlag = false
          this.showOptionFlag = !this.showOptionFlag
        } else {
          this.showOptionFlag = false
        }
      })
    },
    // 显示选项
    onSelect () {
      this.bubblingFlag = true
      this.globalClick(() => {
        if (this.showOptionFlag === true) {
          this.showOptionFlag = false
        } else {
          this.showOptionFlag = !this.showOptionFlag
        }
        if (this.bubblingFlag === true) {
          this.bubblingFlag = false
        } else {
          this.showOptionFlag = false
        }
      })
    },
    // 添加选项
    onOption (item, val) {
      if (this.showRadio) {
        this.selectText = item
        this.$emit('change', this.selectText)
      } else if (this.selectList.indexOf(item) === -1) {
        this.selectList.push(item)
        this.selectListIndex.push(val)
        this.$emit('change', this.selectListIndex)
      }
      this.showOptionFlag = false
    },
    // 删除选项
    onDelete (index) {
      this.bubblingFlag = false
      this.selectList.splice(index, 1)
      this.selectListIndex.splice(index, 1)
      if (this.showRadio) {
        this.$emit('change', this.selectText)
      } else {
        this.$emit('change', this.selectListIndex)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.userSelect{
  position: relative;
  .select{
    display: inline-block;
    width: 2rem;
    border: 1px solid #ccc;
    border-radius: .05rem;
    font-size: .14rem;
    font-weight: 500;
    color: #333;
    line-height: .22rem;
    min-height: .4rem;
    padding: .05rem .2rem 0 .05rem;
    position: relative;
    span{
      font-size: .12rem;
      font-weight: 500;
      color: #333;
      line-height: .2rem;
      background-color: #eee;
      border-radius: .05rem;
      padding: .02rem .05rem;
      display: inline-block;
      margin: 0 .05rem .05rem 0;
      i{
        font-style: normal;
        font-size: .12rem;
        font-weight: 500;
        color: #333;
        line-height: .13rem;
        background-color: #ccc;
        border-radius: 500px;
        width: .16rem;
        height: .16rem;
        display: inline-block;
        text-align: center;
        vertical-align: middle;
        margin-top: -.02rem;
      }
    }
    img{
      display: inline-block;
      width: .06rem;
      height: .12rem;
      transform: rotate(90deg) translateX(-50%);
      position: absolute;
      top: 45%;
      right: .15rem;
    }
  }
  label{
    display: inline-block;
    width: 2rem;
    border: 1px solid #ccc;
    border-radius: .05rem;
    padding: 0 .2rem;
    line-height: .4rem;
    height: .4rem;
    position: relative;
    background-color: #fff;
    font-size: 0;
    input{
      display: inline-block;
      width: 100%;
      height: 100%;
      font-size: .14rem;
      font-weight: 500;
      color: #333;
      line-height: .24rem;
    }
    input:disabled {
      background-color: #fff;
    }
    img{
      width: .06rem;
      height: .12rem;
      position: absolute;
      top: 45%;
      right: .15rem;
      transform: rotate(90deg) translateX(-50%);
      background-color: #fff;
    }
  }
  .option{
    position: absolute;
    top: 100%;
    left: 0;
    width: 2rem;
    background-color: #fff;
    border: 1px solid #ccc;
    padding: .03rem 0;
    border-radius: .05rem;
    z-index: 100;
    .optionItem{
      display: block;
      font-size: .14rem;
      font-weight: 500;
      color: #333;
      line-height: .22rem;
      padding: .02rem .05rem;
    }
    .optionItem:hover{
      background-color: #15A29C;
      color: #fff;
    }
  }
}
</style>
